﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace Pyramid_Game
{
    class CatchMummy : GameScreen
    {
        #region Declarations
        //잡은 수
        int hit=0;
        //놓친 수
        int miss = 0;
        //지나간 횟수
        int count=0;
        //두더지 잡는 횟수
        int maxCount = 10;
        //한번에 나오는 두더지 수
        int numOfMymmy = 2;

        Mummy[] mummy = new Mummy[6];
        List<Mummy> mummyList = new List<Mummy>();
        List<Mummy> hitList = new List<Mummy>();
        Random rand = new Random();

        bool generate = false;
        bool end = false;

        double generateTime;
        //두더지 나오는 간격
        double interval = 2;
        //두더지 사라지는 시간
        double removeTime = 2;

        Texture2D background;
        Texture2D mummyUp;
        Texture2D mummyHit;

        #endregion

        #region Load

        public CatchMummy()
        {
            int firstMummyHeight = 30;
            int secondMummyHeight = 250;
            int firstMummyWidth = 50;
            int secondMummyWidth = 300;
            int thirdMummyWidth = 550;
            int mummySize = 200;

            mummy[0] = new Mummy(new Rectangle(firstMummyWidth, firstMummyHeight, mummySize, mummySize));
            mummy[1] = new Mummy(new Rectangle(secondMummyWidth, firstMummyHeight, mummySize, mummySize));
            mummy[2] = new Mummy(new Rectangle(thirdMummyWidth, firstMummyHeight, mummySize, mummySize));
            mummy[3] = new Mummy(new Rectangle(firstMummyWidth, secondMummyHeight, mummySize, mummySize));
            mummy[4] = new Mummy(new Rectangle(secondMummyWidth, secondMummyHeight, mummySize, mummySize));
            mummy[5] = new Mummy(new Rectangle(thirdMummyWidth, secondMummyHeight, mummySize, mummySize));

            AddMummy();
        }
        public override void LoadContent(ContentManager content)
        {
            background = content.Load<Texture2D>("Texture/mummy/catchmummy");
            mummyUp = content.Load<Texture2D>("Texture/mummy/mummyUp");
            mummyHit = content.Load<Texture2D>("Texture/mummy/mummyHit");
        }

        #endregion

        #region Update & Draw

        public override void Update(Touch touch, GameTime gameTime)
        {
            CheckEnd(gameTime); 

            //mummy를 선택
            if (generate)
            {
                for (int i = 0; i < numOfMymmy; i++)
                {
                    generateTime = gameTime.TotalGameTime.TotalSeconds;

                    int num = rand.Next(0, 6);

                    if (mummyList.Contains(mummy[num]))
                    {
                        i--;
                    }
                    else if (hitList.Contains(mummy[num]))
                    {
                        i--;
                    }
                    else
                    {
                        mummy[num].GenerateTime = generateTime;
                        mummyList.Add(mummy[num]);
                    }
                }
                generate = false;
                count++;
            }
            else
            {
                if (generateTime + interval < gameTime.TotalGameTime.TotalSeconds)
                {
                    generate = true;
                }
            }

            for (int i = mummyList.Count() - 1; i >= 0; i--)
            {
                if (mummyList[i].GenerateTime + removeTime < gameTime.TotalGameTime.TotalSeconds)
                {
                    mummyList.RemoveAt(i);
                    miss++;
                }
            }
            
            HandleInput(touch, gameTime);
            ClearHitList(gameTime);
        }
        public void HandleInput(Touch touch, GameTime gameTime)
        {
            foreach (GestureSample gesture in touch.Gestures)
            {
                if (gesture.GestureType == GestureType.Tap)
                {
                    Point tapLocation = new Point((int)gesture.Position.X, (int)gesture.Position.Y);

                    for (int i = mummyList.Count() - 1; i >= 0; i--)
                    {
                        if (tapLocation.X > mummyList[i].DrawRect.X &&
                        tapLocation.X < mummyList[i].DrawRect.Width + mummyList[i].DrawRect.X &&
                        tapLocation.Y > mummyList[i].DrawRect.Y &&
                        tapLocation.Y < mummyList[i].DrawRect.Height + mummyList[i].DrawRect.Y)
                        {
                            mummyList[i].HitTime = gameTime.TotalGameTime.TotalSeconds;
                            hitList.Add(mummyList[i]);
                            mummyList.RemoveAt(i);
                            hit++;
                        }
                    }
                }
            }
        }
        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            spriteBatch.Draw(background, new Rectangle(0, 0, Game1.viewport.Width, Game1.viewport.Height), Color.White);

            foreach (Mummy m in mummyList)
                spriteBatch.Draw(mummyUp, m.DrawRect, Color.White);
            foreach(Mummy m in hitList)
                spriteBatch.Draw(mummyHit, m.DrawRect, Color.White);
        }

        #endregion

        #region HelperMethods

        public void CheckEnd(GameTime gameTime)
        {
            if (end)
            {
                if (generateTime + 2 < gameTime.TotalGameTime.TotalSeconds)
                {
                    screenManager.AddScreen(new resultScreen(hit, maxCount * numOfMymmy));
                    ScreenManager.RemoveScreen(this);
                }
            }

            if (count >= maxCount)
                generate = false;
            if (count >= maxCount && !end && mummyList.Count() == 0)
            {
                end = true;
                generateTime = gameTime.TotalGameTime.TotalSeconds;
            }
        }
        public void ClearHitList(GameTime gameTime)
        {
            for (int i = hitList.Count()-1; i >= 0; i--)
            {
                if (hitList[i].HitTime + 0.5 < gameTime.TotalGameTime.TotalSeconds)
                {
                    hitList.RemoveAt(i);
                }
            }
        }
        public void AddMummy()
        {
            if (GameConstants.stage == 1)
            {
                //두더지 나오는 간격
                interval = 2;
                //두더지 사라지는 시간
                removeTime = 2;
                //두더지 잡는 횟수
                maxCount = 10;
            }
            else if (GameConstants.stage == 2)
            {
                interval = 2;
                removeTime = 2;
                maxCount = 10;
            }
            else if (GameConstants.stage == 3)
            {
                interval = 2;
                removeTime = 2;
                maxCount = 10;
            }
        }
        
        #endregion
    }
}
